package com.shupian.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shupian.constants.SystemConstants;
import com.shupian.domain.ResponseResult;
import com.shupian.domain.entity.Menu;
import com.shupian.domain.vo.MenuAdminVo;
import com.shupian.domain.vo.MenuUpdateVo;
import com.shupian.domain.vo.MenusVo;
import com.shupian.mapper.MenuMapper;
import com.shupian.service.MenuService;
import com.shupian.utils.BeanCopyUtils;
import com.shupian.utils.SecurityUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 菜单权限表(Menu)表服务实现类
 *
 * @author makejava
 * @since 2022-08-28 11:13:28
 */
@Service("menuService")
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {



    @Override
    public List<String> selectPermsByUserId(Long id) {
        // 如果是管理员，返回所有权限
        if(SecurityUtils.isAdmin()){
            LambdaQueryWrapper<Menu> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.in(Menu::getMenuType, SystemConstants.MENU_,SystemConstants.BUTTON);
            lambdaQueryWrapper.eq(Menu::getStatus,SystemConstants.STATUS_NORMAL);

            List<Menu> menuList = list(lambdaQueryWrapper);
            List<String> perms = menuList.stream().map(Menu::getPerms).collect(Collectors.toList());
            return perms;
        }
        //否则返回所具有的权限
        return getBaseMapper().selectPermsByUserId(id);
    }

    @Override
    public List<Menu> selectRoleKeyByUserId(Long userId) {
        MenuMapper menuMapper = getBaseMapper();
        List<Menu> menus = null;
        //判断是否是管理员
        if(SecurityUtils.isAdmin()){
            //如果是 获取所有符合要求的menu
           menus= menuMapper.selectAllRouterMenu();

        }else {
            //否则获取当前用户所具有的menu
            menus = menuMapper.selectRouterMenuTreeByUserId(userId);
        }
        //构建tree
        //先找出第一层菜单 然后去找出他们的子菜单设置到children属性中
        List<Menu> menuTree = builderMenuTree(menus,0L);


        return menuTree;
    }

    @Override
    public ResponseResult getAllMenu() {
        LambdaQueryWrapper<Menu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(Menu::getParentId).orderByAsc(Menu::getOrderNum);
        List<Menu> menus = list(queryWrapper);
        List<MenuAdminVo> menuAdminVos = BeanCopyUtils.copyBeanList(menus, MenuAdminVo.class);
        return ResponseResult.okResult(menuAdminVos);
    }

    @Override
    public ResponseResult getMenuById(Integer id) {
        Menu menu = getById(id);
        MenuUpdateVo menuUpdateVo = BeanCopyUtils.copyBean(menu, MenuUpdateVo.class);
        return ResponseResult.okResult(menuUpdateVo);
    }

    @Override
    public ResponseResult roleMenuTreeselect(Long id) {
        MenuMapper menuMapper = getBaseMapper();
        List<Menu> menus = null;
        //判断是否是管理员
        if (SecurityUtils.isAdmin()) {
            //如果是 获取所有符合要求的menu
            menus = menuMapper.selectAllRouterMenu();

        } else {
            //否则获取当前用户所具有的menu
            menus = menuMapper.selectRouterMenuTreeByUserId(id);
        }
        List<MenusVo> menusVos = BeanCopyUtils.copyBeanList(menus, MenusVo.class);
        List<MenusVo> menuTree = builderMenuTreeList(menusVos, 0L);



        Map<String, Object> map = new HashMap<>();
        map.put("menus",menuTree);
        List<String> list = menuMapper.selectKeysByUserId(id);
        map.put("checkedKeys",list);


        return ResponseResult.okResult(map);
    }

    private List<MenusVo> builderMenuTreeList(List<MenusVo> menusVos, long parentId) {
        List<MenusVo> menuTree = menusVos.stream()
                .filter(menu -> menu.getParentId().equals(parentId))
                .map(menu -> menu.setChildren(getChildrenList(menu, menusVos)))
                .collect(Collectors.toList());
        return menuTree;
    }

    private List<MenusVo> getChildrenList(MenusVo menu, List<MenusVo> menusVos) {

        List<MenusVo> childrenList = menusVos.stream()
                .filter(m -> m.getParentId().equals(menu.getId()))
                .map(m -> m.setChildren(getChildrenList(m, menusVos)))
                .collect(Collectors.toList());
        return childrenList;
    }


    private List<Menu> builderMenuTree(List<Menu> menus,Long parentId) {
        List<Menu> menuTree = menus.stream()
                .filter(menu -> menu.getParentId().equals(parentId))
                .map(menu -> menu.setChildren(getChildren(menu, menus)))
                .collect(Collectors.toList());
        return menuTree;
    }

    /**
     * 获取传入参数的子menu
     *
     * @param menu
     * @param menus
     * @return
     */
    private List<Menu> getChildren(Menu menu, List<Menu> menus) {
        List<Menu> childrenList = menus.stream()
                .filter(m -> m.getParentId().equals(menu.getId()))
                .map(m -> m.setChildren(getChildren(m,menus)))
                .collect(Collectors.toList());
        return childrenList;
    }
}

